package com.dhj.yygh.controller;

import com.dhj.yygh.common.result.MyResult;
import com.dhj.yygh.model.cmn.Dict;
import com.dhj.yygh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "数据字典的相关接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    DictService dictService;

    @ApiOperation("根据 id 查询其子 id 的数据")
    @GetMapping("/getChildDataById/{id}")
    public MyResult<List<Dict>> getChildDataById(@PathVariable("id") Long id) {
        List<Dict> dicts = dictService.getChildDataById(id);

        if (dicts.size() > 0) {
            return MyResult.ok(dicts);
        }
        return MyResult.fail(201, "没有查询到数据", null);
    }


    @ApiOperation("数据字典导出")
    @GetMapping("/exportDictData")
    public void exportDict(HttpServletResponse resp) {
        dictService.exportDictData(resp);
    }

    @ApiOperation("数据字典导入数据库")
    @PostMapping("/importDictData")
    public MyResult importDict(MultipartFile file) {
        dictService.importDictData(file);
        return MyResult.ok("导入成功!");
    }

    @ApiOperation("根据 dictcode && value 值查询数据字典")
    @GetMapping("/getDictByDictcodeAndValue/{dictcode}/{value}")
    public String getDictByDictcodeAndValue(
            @PathVariable("dictcode") String dictcode,
            @PathVariable("value") String value) {

        String name = dictService.getDictDataByDictcodeAndValue(dictcode, value);

        return name;
    }

    @ApiOperation("根据 value 值查询数据字典")
    @GetMapping("/getDictByValue/{value}")
    public String getDictByValue(@PathVariable("value") String value) {

        String name = dictService.getDictByValue(value);

        return name;
    }

    @ApiOperation("根据 dict_code 查询其下级列表")
    @GetMapping("/getChildByDictCode/{dict_code}")
    public MyResult getChild(@PathVariable("dict_code") String dict_code) {

        List<Dict> dicts = dictService.getChildByDictCode(dict_code);

        return MyResult.ok(dicts);
    }
}
